/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gee.util;

import javax.microedition.lcdui.*;

/**
 * 
 * @author CoCo
 */
public class Ui {

	/**
	 * drawClip 画一个图片的一部分,并支持翻转，用于MIDP1.0中
	 * 
	 * @param g
	 *            Graphics
	 * @param x
	 *            int 要画的位置x
	 * @param y
	 *            int 要画的位置y
	 * @param image
	 *            Image 图片
	 * @param clipX
	 *            int clip在图片中的坐标x
	 * @param clipY
	 *            int clip在图片中的坐标y
	 * @param clipWidth
	 *            int clip在图片中的宽度
	 * @param clipHeight
	 *            int clip在图片中的高度
	 * @param mode
	 *            int 0:不做任何转换 0x2000：绕Y轴镜象翻转 0x4000:绕X轴镜象翻转
	 */
	public static void drawClipImageForMIDP1(Graphics g, int x, int y,
			Image image, int clipX, int clipY, int clipWidth, int clipHeight,
			int mode) {
		int oldWidth = g.getClipWidth();
		int oldHeight = g.getClipHeight();
		if (mode == 0) {// 不作旋转
			g.setClip(x, y, clipWidth, clipHeight);
			g.drawImage(image, x - clipX, y - clipY, 0);
		} else if (mode == 0x2000) {// 绕Y轴镜象翻转
			for (int i = 0; i < clipWidth; i++) {
				g.setClip(x + i, y, 1, clipHeight);
				g.drawImage(image, x + i - clipX - clipWidth + i + 1,
						y - clipY, 0);
			}
		} else if (mode == 0x4000) {// 绕X轴镜象翻转
			for (int i = 0; i < clipHeight; i++) {
				g.setClip(x, y + i, clipWidth, 1);
				g.drawImage(image, x - clipX, y + i * 2 - clipY - clipHeight
						+ 1, 0);
			}
		}
		g.setClip(0, 0, oldWidth, oldHeight);
	}

	/*
	 * @param g 画笔
	 * 
	 * @param Fillcolor 圆角矩形填充颜色
	 * 
	 * @param DrawColor 圆角矩形边缘颜色
	 * 
	 * @param x 圆角矩形x坐标
	 * 
	 * @param y 圆角矩形y坐标
	 * 
	 * @param WidthFill
	 * 
	 * @param HeightFill
	 * 
	 * @param WidthDraw
	 * 
	 * @param HeightDraw
	 * 
	 * @param artR
	 */
	public static void paintRoundRect(Graphics g, int FillColor, int DrawColor,
			int x, int y, int WidthFill, int HeightFill, int WidthDraw,
			int HeightDraw, int artR) {
		g.setColor(FillColor);
		g.fillRoundRect(x, y, WidthFill, HeightFill, artR, artR);
		g.setColor(DrawColor);
		g.drawRoundRect(x, y, WidthDraw, HeightDraw, artR, artR);
	}

	/**
	 * int x ,---- 坐标 int y,----- 坐标 int w ,----- 宽 int h ,----- 高 int level
	 * ,------渐变等级 int startColor ,-----渐变色1 int endColor ,------渐变色2 boolean
	 * vertical-----是否垂直渐变
	 */
	// 颜色渐变代码
	public static void fillGradualColor(Graphics g, int x, int y, int w, int h,
			int level, int startColor, int endColor, boolean vertical) {
		int time = (vertical ? w / level : h / level);
		int sr = ((startColor & 0xFF0000) >> 16) & 0xFF;
		int sg = ((startColor & 0xFF00) >> 8) & 0xFF;
		int sb = (startColor & 0xFF);
		// System.out.println("start:"+sr+" "+sg+" "+sb);
		int er = ((endColor & 0xFF0000) >> 16) & 0xFF;
		int eg = ((endColor & 0xFF00) >> 8) & 0xFF;
		int eb = (endColor & 0xFF);
		// System.out.println("end:"+er+" "+eg+" "+eb);
		int red = (er - sr);
		int green = (eg - sg);
		int blue = (eb - sb);
		// System.out.println("level:"+red+" "+green+" "+blue);
		for (int i = 0; i <= time; i++) {
			int cr = sr + red * i / time;
			int cg = sg + green * i / time;
			int cb = sb + blue * i / time;
			if (i == time) {
				cr = (endColor & 0xFF0000) >> 16;
				cg = (endColor & 0xFF00) >> 8;
				cb = endColor & 0xFF;
			}
			// System.out.println("c["+i+"]:"+cr+" "+cg+" "+cb);
			g.setColor(cr, cg, cb);
			g.fillRect(x + (vertical ? i * level : 0), y
					+ (vertical ? 0 : i * level), vertical ? level : w,
					vertical ? h : level);
		}
	}

	// 文字阴影
	public static void drawFontWithShadow(Graphics g, String str, int x, int y,
			int frontColor, int backColor, int anchor) {
		g.setColor(backColor);
		g.drawString(str, x - 1, y - 1, anchor);
		g.drawString(str, x - 1, y + 1, anchor);
		g.drawString(str, x + 1, y - 1, anchor);
		g.drawString(str, x + 1, y + 1, anchor);
		g.setColor(frontColor);
		g.drawString(str, x, y, anchor);
	}

	/* 画艺术字 */

	public static void drawStringShadow(Graphics g, int mainColor,
			int topColor, int bottomColor, String str, int x, int y, int anchor) {
		if (topColor >= 0) {
			g.setColor(topColor);
			// g.setFont(StaticConst.L_M_B);
			g.drawString(str, x - 1, y, anchor);
			g.drawString(str, x, y - 1, anchor);
			g.drawString(str, x - 1, y - 1, anchor);
		}
		if (bottomColor >= 0) {
			g.setColor(bottomColor);
			// g.setFont(StaticConst.L_M_B);
			g.drawString(str, x + 1, y, anchor);
			g.drawString(str, x, y + 1, anchor);
			g.drawString(str, x + 1, y + 1, anchor);
		}
		g.setColor(mainColor);
		// g.setFont(StaticConst.L_M_B);
		g.drawString(str, x, y, anchor);
	}

	/*
	 * @param g 画笔
	 * 
	 * @param Fillcolor 圆角矩形填充颜色
	 * 
	 * @param DrawColor 圆角矩形边缘颜色
	 * 
	 * @param x 圆角矩形x坐标
	 * 
	 * @param y 圆角矩形y坐标
	 * 
	 * @param WidthFill
	 * 
	 * @param HeightFill
	 * 
	 * @param WidthDraw
	 * 
	 * @param HeightDraw
	 */

	public static void paintRect(Graphics g, int FillColor, int DrawColor,
			int x, int y, int WidthFill, int HeightFill, int WidthDraw,
			int HeightDraw) {
		g.setColor(FillColor);
		g.fillRect(x, y, WidthFill, HeightFill);
		g.setColor(DrawColor);
		g.drawRect(x, y, WidthDraw, HeightDraw);
	}

	/*
	 * 绘制半透明框
	 */

	public static void DrawAlphaRect(int x, int y, int width, int height,
			int color, Graphics g) {
		int array[] = new int[height * width];
		for (int i = 0; i < array.length; i++) {
			array[i] = color;
		}
		g.drawRGB(array, 0, width, x, y, width, height, true);
		g.setColor(color);
		g.drawRoundRect(x - 1, y - 1, width + 1, height + 1, 10, 10);
		g.drawRoundRect(x - 2, y - 2, width + 3, height + 3, 10, 10);
	}

	private static int tipStringPos = StaticConst.SC_WIDTH - 50; // 当前字符串左边的位置
	private static int tipStringSpeed = 4; // 字符串移动速度
	private static final int FONT_HEIGHT = 11; // 字体高度 font.getHeight() 不准确

	/**
	 * 滚动文字效果
	 * 
	 * @param Graphics
	 *            g - 画刷
	 * @param String
	 *            str - 所画字符串
	 * @param int height - 字符串高度
	 * @param int rectX - 剪裁区顶点X坐标
	 * @param int rectY - 剪裁区顶点Y坐标
	 * @param int rectWidth - 剪裁区宽度
	 * @param int rectHeight - 剪裁区高度 TIPSTR_LEFT //左边消失绘制坐标 TIPSTR_RIGHT
	 *        //右边出现绘制坐标
	 */
	public static void drawTipString(Graphics g, String str, int height,
			int TIPSTR_LEFT, int TIPSTR_RIGHT, int rectX, int rectY,
			int rectWidth, int rectHeight, int color) {
		g.setColor(color);
		int strWidth = g.getFont().stringWidth(str);
		int strHeight = FONT_HEIGHT;
		tipStringPos -= tipStringSpeed;
		if (tipStringPos + strWidth < TIPSTR_LEFT) {
			tipStringPos = TIPSTR_RIGHT;
		}
		// 裁减区
		int oldClipX = g.getClipX();
		int oldClipY = g.getClipY();
		int oldClipWidth = g.getClipWidth();
		int oldClipHeight = g.getClipHeight();
		g.setClip(rectX, rectY, rectWidth, rectHeight);
		g.drawString(str, tipStringPos, height - strHeight / 2, Graphics.LEFT
				| Graphics.TOP);
		g.setClip(oldClipX, oldClipY, oldClipWidth, oldClipHeight);
		// System.out.println("1111");
	}

	public static void drawImage(Image src, int x_src, int y_src, int width,
			int height, int x_dest, int y_dest, int anchor, Graphics g) {
		g.drawRegion(src, x_src, y_src, width, height, 0, x_dest, y_dest,
				anchor);
	}

	private static int width;
	private static int height;
	private static char cha;

	/**
	 * 画数字图片
	 * 
	 * @param imgNumber
	 * @param number
	 * @param x
	 * @param y
	 * @param g
	 * @param type
	 */
	public static void drawNumber(Graphics g, Image imgNumber, int number,
			int x, int y, int w, int type) {
		width = imgNumber.getWidth() / w;
		height = imgNumber.getHeight();
		String strNmber = Integer.toString(number);
		char[] chrNumr = strNmber.toCharArray();
		cha = 0;
		if (type == StaticConst.NUMBER_LEFT) {
			x -= (chrNumr.length - 1) * width;
		} else if (type == StaticConst.NUMBER_UP) {
			y -= chrNumr.length * height;
		}
		for (int i = 0; i < chrNumr.length; i++) {
			cha = chrNumr[i];
			switch (cha) {
			case '0':
				Ui.drawImage(imgNumber, 0 * width, 0, width, height, x, y, 0, g);
				break;
			case '1':
				Ui.drawImage(imgNumber, 1 * width, 0, width, height, x, y, 0, g);
				break;
			case '2':
				Ui.drawImage(imgNumber, 2 * width, 0, width, height, x, y, 0, g);
				break;
			case '3':
				Ui.drawImage(imgNumber, 3 * width, 0, width, height, x, y, 0, g);
				break;
			case '4':
				Ui.drawImage(imgNumber, 4 * width, 0, width, height, x, y, 0, g);
				break;
			case '5':
				Ui.drawImage(imgNumber, 5 * width, 0, width, height, x, y, 0, g);
				break;
			case '6':
				Ui.drawImage(imgNumber, 6 * width, 0, width, height, x, y, 0, g);
				break;
			case '7':
				Ui.drawImage(imgNumber, 7 * width, 0, width, height, x, y, 0, g);
				break;
			case '8':
				Ui.drawImage(imgNumber, 8 * width, 0, width, height, x, y, 0, g);
				break;
			case '9':
				Ui.drawImage(imgNumber, 9 * width, 0, width, height, x, y, 0, g);
				break;
			}
			switch (type) {
			case StaticConst.NUMBER_LEFT:
			case StaticConst.NUMBER_RIGHT:
				x += width;
				break;
			case StaticConst.NUMBER_UP:
			case StaticConst.NUMBER_DOWN:
				y += height;
				break;
			}

		}
		strNmber = null;
		chrNumr = null;
		System.gc();
	}

	public static int temp[] = new int[50000];
	public static final int BLUR_LEFT = 0x1;
	public static final int BLUR_RIGHT = 0x2;
	public static final int BLUR_BOTH = BLUR_LEFT | BLUR_RIGHT;

	/**
	 * 画模糊效果 src:源图 x,y,width,height:画的区域 blurAmount:模糊度 desAmount:饱和度
	 * blurType:模糊方向
	 */
	public static void paintBlurEffect(Image src, Graphics g, int x, int y,
			int width, int height, int blurAmount, int desAmount, int blurType) {
		try {
			int buf[] = temp;
			int d = blurAmount % 128;
			int maxBlock = temp.length / width;
			int block = 80;
			int yy = 0;
			boolean done = false;
			while (!done) {
				block = height - yy;
				if (block > maxBlock) {
					block = maxBlock - 1;
				} else {
					done = true;
				}
				int size = width * block;
				src.getRGB(buf, 0, width, x, y + yy, width, block);
				blurDesaturateBlock(size, buf, d, desAmount, blurType);
				g.drawRGB(buf, 0, width, x, y + yy, width, block, false);
				yy += block;
			}
		} catch (Exception e) {
		}
	}

	static void blurDesaturateBlock(int size, int buf[], int blurAmount,
			int desAmount, int burType) {
		int r1 = 0;
		int b1 = 0;
		int r1b1 = 0;
		int g1 = 0;
		// blur in both sides
		// right to left
		if ((burType & BLUR_LEFT) == BLUR_LEFT) {
			for (int k = size - 1; k >= 0; k--) {
				int c2 = buf[k];
				int r2b2 = (c2) & 0xFF00FF;
				int g2 = (c2) & 0xFF00;
				r1b1 = (r2b2 + ((r1b1 - r2b2) * blurAmount >> 7)) & 0xFF00FF;
				g1 = (g2 + ((g1 - g2) * blurAmount >> 7)) & 0x00FF00;
				int color = r1b1 | g1;
				buf[k] = color;
			}
		}
		if ((burType & BLUR_RIGHT) == BLUR_RIGHT) {
			for (int k = 0; k < size; k++) {
				int c2 = buf[k];
				int r2 = (c2) & 0xFF0000;
				int g2 = (c2) & 0xFF00;
				int b2 = (c2) & 0xFF;
				r1 = r2 + ((r1 - r2) * blurAmount >> 7);
				g1 = g2 + ((g1 - g2) * blurAmount >> 7);
				b1 = b2 + ((b1 - b2) * blurAmount >> 7);
				// the desaturated components
				int lum = ((r1 >> 16) + (g1 >> 8) + b1 + 16) >> 2; // the
				// desaturated
				// col
				// (luminosity)
				int desR = (lum << 16);
				int desG = (lum << 8);
				int desB = (lum);
				desR = r1 + ((desR - r1) * desAmount >> 7);
				desG = g1 + ((desG - g1) * desAmount >> 7);
				desB = b1 + ((desB - b1) * desAmount >> 7);
				int color = (desR & 0xFF0000) | (desG & 0xFF00) | desB;
				buf[k] = color;
			}
		}
	}
}
